// pages/rfid_binds/maintain_loc/maintain_loc.js
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
const mybehavior = require("../../../behaviors/requests")

function generate_list(code, num) {
  var result = [];
  for (var i = 1; i <= num; i = i + 1) {
    result.push(code + i);
  }
  return result;
}
const citys = {
  "A": generate_list("A", 41),
  "B": generate_list("B", 31),
  "C": generate_list("C", 26),
  "D": generate_list("D", 22),
  "E": generate_list("E", 6),
  "F": generate_list("F", 35),
}
Page({
  behaviors: [mybehavior],
  /**
   * 页面的初始数据
   */
  data: {
    rfid: "",
    loc_code: null,
    columns: [{
        values: Object.keys(citys),
        className: 'column1',
      },
      {
        values: citys['A'],
        className: 'column2',
        defaultIndex: 2,
      },
    ],
    showPicker: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  onChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    console.log(event.detail);
    picker.setColumnValues(1, citys[value[0]]);
  },
  onSubmit() {
    let that = this;
    Dialog.confirm({
        title: "信息确认",
        message: "rfid标签号:" + this.data.rfid + "\n位置编号:" + this.data.loc_code
      })
      .then(() => {
        that.post("https://logisticsengineering.gree.com/rfid_api/rfid_manage/maintain_location", {
          rfid: this.data.rfid,
          loc_code: this.data.loc_code
        }).then(res => {
          console.log(res);
          if (res.code == 20000) {
            wx.showToast({
              title: '成功!',
            })
            that.rfid = "";
            that.loc_code = "";
          } else {
            wx.showToast({
              title: res.msg,
              icon: "error"
            })
          }
        }).catch((e) => {
          wx.showToast({
            title: e.msg,
            icon: "error"
          })
        })
      }).catch(() => {
        wx.showToast({
          title: '取消操作!',
        })
      })
  },
  setShowFalse() {
    this.setData({
      showPicker: false
    })
  },
  setShowTrue() {
    this.setData({
      showPicker: true
    })
  },
  scan() {
    const self = this;
    wx.scanCode().then((res) => {
      const hardware_code = res.result;
      self.setData({
        rfid: hardware_code
      });
    });
  },
  onClear() {
    Dialog.confirm({
      title: "数据重置",
      message: "确认数据重置？"
    }).then(() => {
      this.rfid = "";
      this.loc_code = null;
      Toast.success("数据已清零");
    });
  },
  onConfirm(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    console.log(value);
    this.setData({
      loc_code: value[1],
      showPicker: false
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})